package Core;

public class CharacterLevelerImpl implements CharacterLeveler
{
	private SnapshotApplier _snapshotApplier;
		
	public CharacterLevelerImpl(SnapshotApplier snapshotApplier)
	{
		this._snapshotApplier = snapshotApplier;
	}
	
	@Override
	public void levelUp(Character character, CharacterClass newClass) throws RequirementsNotMetException
	{
		int nextLevel = this.getNewLevel(character, newClass);
		
		if (this.checkIfCharacterCanLevel(character, newClass, nextLevel))
		{
			this.updateCareer(character.getCareer(), newClass);
			this.updateCharacter(character, newClass.findSnapshotByLevel(nextLevel));
		}
		else
		{
			throw new RequirementsNotMetException();
		}
	}
	
	private boolean checkIfCharacterCanLevel(Character character, CharacterClass newClass, int newLevel)
	{
		boolean canLevel = newClass.canLevel(character, newLevel);
				
		return canLevel;
	}

	private int getNewLevel(Character character, CharacterClass newClass)
	{
		int newLevel = character.getLevelsIn(newClass) + 1;
		
		return newLevel;
	}
	
	private void updateCareer(CharacterCareer career, CharacterClass newClass)
	{
		career.levelUp(newClass);
	}

	private void updateCharacter(Character character, LevelSnapshot snapshot)
	{
		this._snapshotApplier.applySnapshotTo(character, snapshot);
	}
}
